package com.personal.ifanya.domain;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tbwork.utility.toolbox.tbConvert;
import org.tbwork.utility.toolbox.tbDate;
import org.tbwork.utility.toolbox.tbString;

import com.personal.ifanya.application.dto.NewsDTO;
import com.personal.ifanya.infrastructure.dal.dao.ItemDAO;
import com.personal.ifanya.infrastructure.dal.entity.ItemDO;


@Component
public class NewsBiz {

	
	@Autowired
	private ItemDAO itemDAO;
	
	public int insert(NewsDTO newsDTO)
	{ 
		ItemDO itemDO = cast2ItemDO(newsDTO);
		
		return itemDAO.insert(itemDO); 
	}
	
	public NewsDTO getByItemId(int itemId)
	{
		ItemDO itemDO=  itemDAO.loadById(itemId);
		if(itemDO!=null)
			return cast2NewsDTO(itemDO, true);
		else
			return null;
		
	}
	public List<NewsDTO> getNewsList(int type)
	{
		List<NewsDTO> result = new ArrayList<NewsDTO>();
		List<ItemDO> itemList= itemDAO.selectAll(type);
		
		if(itemList!=null)
		{
			for(ItemDO item :itemList)
			{
				result.add(cast2NewsDTO(item, false));
			}
		}
		return result;
	}
	
	public  List<NewsDTO>  getLatestNews()
	{ 
		List<NewsDTO> result = new ArrayList<NewsDTO>();
		
		for(int i=1; i<= 7;i++)
		{
			ItemDO item= itemDAO.selectLastest(i);
			if(item!=null)
			{
				 result.add( cast2NewsDTO(item, false));
			}
		}  
		return result;
	}
	
	
	public static void main(String[] args) {
		
		String dateString = "2015-08-19 00:00:00";
		
		System.out.println(Timestamp.valueOf(dateString));
		
	}
	
	private ItemDO cast2ItemDO(NewsDTO newsDTO)
	{
		ItemDO itemDO = new ItemDO();
		itemDO.setComment(newsDTO.getComment());
		itemDO.setContent(newsDTO.getContent());
		itemDO.setHow(newsDTO.getHow());
		Timestamp temp = new Timestamp(0); 
		String dateString ="";
		if(newsDTO.getIssueDate()!=null && newsDTO.getIssueDate().length()<13)
			dateString = newsDTO.getIssueDate()+" 00:00:00";
		itemDO.setIssueDate(Timestamp.valueOf(dateString));
		itemDO.setSourcelink(newsDTO.getSourcelink());
		itemDO.setTitle(newsDTO.getTitle());
		itemDO.setType(newsDTO.getType());
		itemDO.setWeight(newsDTO.getWeight());
	    return itemDO;
	}
	
	private NewsDTO cast2NewsDTO(ItemDO itemDO, boolean full)
	{
		
		NewsDTO newsDTO = new NewsDTO(); 
		String comment = "";
		if(itemDO.getComment()!=null)
		{ 
			comment = itemDO.getComment();
			if(!full){
				if(itemDO.getComment().length()>100)
				{
					comment = itemDO.getComment().substring(0,100);
				}
				int index = comment.indexOf('<');
				if(index >0)
					comment = comment.substring(0,index);
			}
			
		}
		newsDTO.setComment(comment);
		newsDTO.setContent(itemDO.getContent());
		newsDTO.setHow(itemDO.getHow());
		newsDTO.setId(itemDO.getItemId());
		Timestamp temp = new Timestamp(0); 
		newsDTO.setIssueDate(getDateString(itemDO.getIssueDate()) );
		newsDTO.setSourcelink(itemDO.getSourcelink());
		newsDTO.setTitle(itemDO.getTitle());
		newsDTO.setTypeName(getNewsTypeName(itemDO.getType()));
		newsDTO.setWeight(itemDO.getWeight());
	    return newsDTO;
	}
	
	private String getDateString(Timestamp date)
	{
		return (date.getYear()+1900)+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日";
	}
	
	private String getNewsTypeName(int type)
	{
		switch(type)
		{
			case 1: return "重要新闻";
			case 2: return "昆明前线";
			case 3: return "上海叔叔";
			case 4: return "倡导维权";
			case 5: return "媒体报道";
			case 6: return "泛亚公告";
			case 7: return "危机回顾";
			default:return "重要新闻";
		}
	}
	
}
